package com.xydtech.ecard.user.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.mapper.EcardApiUserMapper;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户信息Service实现类
 */
@Service
public class EcardApiUserServiceImpl implements IEcardApiUserService {

    @Autowired
    private EcardApiUserMapper userMapper;

    /**
     * 获取员工用户信息
     *
     * @param currentUserId
     * @return
     */
    @Override
    public EcardApiUserInfo getUserInfo(String currentUserId, String salt) {
        EcardApiUserInfo userInfo = userMapper.getUserInfo(currentUserId);
        if (userInfo != null){
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(userInfo.getPhoneno())) {
                    userInfo.setPhoneno(AESEncryptionUtils.decrypt(userInfo.getPhoneno(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return userInfo;
    }

    /**
     * 电子名片-用户相关-根据机构ID获取客户经理
     *
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiUserInfo> getUserListByDeptId(String deptId) {
        return userMapper.getUserListByDeptId(deptId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonalInfo(EcardApiUserInfo ecardUserInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardUserInfo.getPhoneno())) {
                ecardUserInfo.setPhoneno(AESEncryptionUtils.encrypt(ecardUserInfo.getPhoneno(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //修改员工表sys_user
        userMapper.updateSysUserInfo(ecardUserInfo);
        //修改员工和岗位关联表sys_user_post
        userMapper.updateSysUserPostInfo(ecardUserInfo);
        //修改微信注册表ecard_regist_info
        userMapper.updateRegistInfo(ecardUserInfo);
    }
}
